package cn.haoxsf.app.system.controller;


import cn.haoxsf.app.common.api.CommonResult;
import cn.haoxsf.app.common.api.enums.SettledInExceptionEnum;

import cn.haoxsf.app.common.api.enums.SystemExceptionEnum;
import cn.haoxsf.app.common.exception.BaseException;
import cn.haoxsf.app.common.util.ToolUtil;
import cn.haoxsf.app.system.dto.LoginUser;
import cn.haoxsf.app.system.entity.SettledIn;
import cn.haoxsf.app.system.model.param.SettledInParam;
import cn.haoxsf.app.system.service.IAuthService;
import cn.haoxsf.app.system.service.IFaultCategoryService;
import cn.haoxsf.app.system.service.ISettledInService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 入驻表 前端控制器
 * </p>
 *
 * @author Ele  qvbanfm@qq.com
 * @since 2020-03-15
 */
@RestController
@RequestMapping("/settled-in")
@Api(tags = "入驻相关请求")
public class SettledInController {
    @Autowired
    private IFaultCategoryService faultCategoryService;
    @Autowired
    private IAuthService authService;
    @Autowired
    private ISettledInService settledInService;

    //返回入驻类型
    @GetMapping("settledInType")
    @ApiOperation(value = "展示入驻类型", notes = "展示入驻类型 用于入驻;注意如果用户已注册，就会返回 用户已申请")
    public CommonResult getSettledInType() {
        LoginUser loginUser = authService.getLoginUser();
        SettledIn member = settledInService.getOne(new QueryWrapper<SettledIn>().eq("member_id", loginUser.getId()));
        if(ToolUtil.isNotEmpty(member)){
            return CommonResult.error(SettledInExceptionEnum.USER_ALREADY_EXISTS);
        }
        return CommonResult.success(faultCategoryService.list());
    }

    //接受入驻
    @PostMapping("in")
    @ApiOperation(value = "保存入驻信息", notes = "保存入驻信息")
    public CommonResult in(@Validated @RequestBody SettledInParam settledIn, BindingResult results) {
        if (results.hasErrors()) {
            return CommonResult.error(SystemExceptionEnum.VALIDATE_FAILED.getCode(), results.getFieldError().getDefaultMessage());
        }
        if (!"6668".equals(settledIn.getSms())) {//TODO 验证码
            return CommonResult.error(401, "短信验证码不正确");
        }
        if ("YES".equals(settledIn.getIsNotStore())) {//有门店时
            //门店照片不能为空
            if (ToolUtil.isEmpty(settledIn.getStoreImageId())) {
                return CommonResult.error(SystemExceptionEnum.VALIDATE_FAILED.getCode(), "有门店必须上传门店图片");
            }
            //营业执照不能为空
            if (ToolUtil.isEmpty(settledIn.getStoerLicense())) {
                return CommonResult.error(SystemExceptionEnum.VALIDATE_FAILED.getCode(), "有门店必须上营业执照");
            }
        }
        if (settledIn.getLatitude() == 0 || settledIn.getLatitude() == 0) {
            return CommonResult.error(SystemExceptionEnum.VALIDATE_FAILED.getCode(), "经纬度不能为0");
        }
        LoginUser loginUser = authService.getLoginUser();
        if(loginUser.getPermissionList().size() != 0){
            throw new BaseException(SettledInExceptionEnum.USER_ALREADY_EXISTS);
        }
        SettledIn entity = new SettledIn();
        BeanUtils.copyProperties(settledIn, entity);
        entity.setMemberId(loginUser.getId());
        entity.setAuditStatus("AUDITED");
        entity.setSubmitTime(new Date());
        entity.setSettledInTypeId(Long.valueOf(settledIn.getSettledInTypeId()));
        settledInService.addSettledIn(entity);
        return CommonResult.success();
    }

    //获取入驻信息
    @ApiOperation(value = "获取入驻信息", notes = "用于获取入驻信息")
    @GetMapping("get")
    public CommonResult get() {
        LoginUser loginUser = authService.getLoginUser();
        return CommonResult.success(settledInService.getOne(loginUser.getId().toString()));
    }
}
